package fnord.jovian.tango;
import java.awt.*;
import java.text.NumberFormat;

import javax.swing.*;

import fnord.jovian.TheBoard;

public class TheTextField implements TheObject {

	private JFormattedTextField _field;
	private TheBoard _board;

	public TheTextField(TheBoard board) {
		_field = new JFormattedTextField();
		_board.add(_field);
		_field.setVisible(false);
        _field.setBorder(javax.swing.BorderFactory.createEmptyBorder());
	}

	public TheTextField(TheBoard board, int w, int h) {
		_board = board;
		_field = new JFormattedTextField();
		_board.add(_field);
		_field.setSize(w,h);
		_field.setVisible(false);
		_field.setBorder(BorderFactory.createLineBorder(TheTangoConstants.GRAY,1));
		_field.setBackground(TheTangoConstants.GRAY);
		_field.setForeground(TheTangoConstants.WHITE);
	}

	@Override
	public void paint(Graphics2D g) {}

	public void setNumbersOnly(boolean num, int places) {
		if (num) {
			NumberFormat format = NumberFormat.getInstance();
			format.setMaximumIntegerDigits(places);
			format.setMaximumFractionDigits(0);
			format.setGroupingUsed(false);
			_field.setVisible(false);
			int w = _field.getWidth();
			int h = _field.getHeight();
			_field = new JFormattedTextField(format);
			_board.add(_field);
			_field.setSize(w,h);
			_field.setVisible(false);
			_field.setBorder(BorderFactory.createLineBorder(TheTangoConstants.GRAY,1));
			_field.setBackground(TheTangoConstants.GRAY);
			_field.setForeground(TheTangoConstants.WHITE);
		}
		else {
			_field.setVisible(false);
			int w = _field.getWidth();
			int h = _field.getHeight();
			_field = new JFormattedTextField();
			_board.add(_field);
			_field.setSize(w,h);
			_field.setVisible(false);
			_field.setBorder(BorderFactory.createLineBorder(TheTangoConstants.GRAY,1));
			_field.setBackground(TheTangoConstants.GRAY);
			_field.setForeground(TheTangoConstants.WHITE);
		}
	}

	public String getValue() {
		return _field.getText();
	}

	public void setValue(String t) {
		_field.setText(t);
	}

	public void setActive(boolean active) {
		_field.setVisible(active);
	}

	public void setEditable(boolean edit) {
		_field.setEditable(edit);
	}
	
	public boolean getActive() {
		return _field.isVisible();
	}

	public double getX() {
		return _field.getX();
	}

	public double getY() {
		return _field.getY();
	}

	public void setLocation(double x, double y) {
		_field.setLocation((int)x, (int)y);
	}

	public double getWidth() {
		return _field.getWidth();
	}

	public double getHeight() {
		return _field.getHeight();
	}

	public void setSize(double w, double h) {
		_field.setSize((int)w,(int)h);
	}

	public double getCenterX() {
		return _field.getX() + _field.getWidth()/2.0;
	}

	public double getCenterY() {
		return _field.getY() + _field.getHeight()/2.0;
	}

	public boolean contains(Point p) {
		return false;
	}

	public boolean containsClick(Point p) {
		return false;
	}

	public boolean containsDrag(Point p) {
		return false;
	}

	public boolean containsUp(Point p) {
		return false;
	}

	public void grabFocus() {
		_field.grabFocus();
	}

	public boolean isFocused() {
		return _field.hasFocus();
	}
	
	public void setGray(boolean active) {
		if (!active) {
			_field.setBorder(BorderFactory.createLineBorder(TheTangoConstants.DARK_GRAY,1));
			_field.setBackground(TheTangoConstants.DARK_GRAY);
		}
		else {
			_field.setBorder(BorderFactory.createLineBorder(TheTangoConstants.GRAY,1));
			_field.setBackground(TheTangoConstants.GRAY);
		}
	}

}
